﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Collections;

namespace ICeFramework.Web.Common
{
    public class ResManager
    {
        private ResourceManager _manager;

        private string _resourceFile;
        public string ResourceFile
        {
            get { return _resourceFile; }
        }

        private CultureInfo _culture;
        public CultureInfo Culture
        {
            get { return _culture; }
        }

        public ResManager(string resourceFile, CultureInfo culture)
        {
            _manager = new ResourceManager(string.Format("{0}_Resources", culture.NativeName), Assembly.GetExecutingAssembly());
            _resourceFile = resourceFile;
            _culture = culture;
        }

        public ResManager(CultureInfo culture)
        {
            _manager = new ResourceManager(string.Format("{0}_Resources", culture.NativeName), Assembly.GetExecutingAssembly());
            _culture = culture;
        }

        public void AddResource(string name, string value)
        {
            using (ResourceWriter writer = new ResourceWriter(_resourceFile))
            {
                try
                {
                    writer.AddResource(name, value);
                }
                finally
                {
                    writer.Close();
                }
            }
        }
        public void AddResource(string name, object value)
        {
            using (ResourceWriter writer = new ResourceWriter(_resourceFile))
            {
                try
                {
                    writer.AddResource(name, value);
                }
                finally
                {
                    writer.Close();
                }
            }
        }
        public void AddResource(string name, byte[] value)
        {
            using (ResourceWriter writer = new ResourceWriter(_resourceFile))
            {
                try
                {
                    writer.AddResource(name, value);
                }
                finally
                {
                    writer.Close();
                }
            }
        }

        public string GetValue(string name)
        {
            return _manager.GetString(name, _culture);
        }

        //public ResourceSet ResourceSet
        //{
        //    get
        //    {

        //        return _manager.GetResourceSet(_culture, false, true);
        //    }
        //}

        public object this[string resourceName]
        {
            get
            {
                return GetValue(resourceName);
            }
        }

        public IDictionaryEnumerator GetEnumerator()
        {
            ResourceReader rdr = new ResourceReader(_resourceFile);
            return rdr.GetEnumerator();
        }

    }
}
